package com.nero.controller;

import com.nero.bean.PO.TbItemCat;
import com.nero.common.utils.JedisClientSingle;
import com.nero.common.utils.SerializeUtils;
import com.nero.service.IItemCatService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

/**
 * Created by Nero on 18/8/10.
 */
@Controller
@RequestMapping("/category")
public class CategoryController {
    private static Logger logger = LogManager.getLogger(CategoryController.class);
    @Resource
    JedisClientSingle jedisClientSingle;

    @Resource
    IItemCatService itemCatServiceImpl;

    @RequestMapping("/getAll")
    @ResponseBody
    public List<TbItemCat> getAll() throws IOException, ClassNotFoundException {
        logger.info("开始查询所有类目");
        List<TbItemCat> list;
        String str = null;
        try {
            str = jedisClientSingle.get("syx_categoryController_getAll");
        } catch (Exception e) {
            list = itemCatServiceImpl.getRoots();
            return list;
        }
        if (str == null) {
            logger.info("从数据库查询出类目");
            list = itemCatServiceImpl.getRoots();
            jedisClientSingle.set("syx_categoryController_getAll", SerializeUtils.serialize(list));
        } else {
            logger.info("从Redis取出类目");
            list = (List<TbItemCat>) SerializeUtils.parse(str);
        }
        return list;
    }
}
